import os
from pyre.core import types
from pyre.data import info
from pyre.gen import terrain

class TerrainInfo(info.InfoDB):

    def __init__(self, filename):
        info.InfoDB.__init__(self, filename, tile=info.parse_tile,
                                             passable=info.parse_bool,
                                             weight=float)

    def get_terrain(self):
        terrain = {}
        for name, info in self.data.iteritems():
            terrain[info['tile'], info['passable']] = info['weight']
        return terrain


class OverworldInfo(info.InfoDB):

    def __init__(self, filename):
        info.InfoDB.__init__(self, filename, tile=info.parse_tile,
                                             passable=info.parse_bool,
                                             height=float)

        self.basedir = os.path.dirname(filename)

    def get_heights(self):
        mapping = []
        for name, info in self.data.iteritems():
            tile = info['tile'], info['passable']
            mapping.append((info['height'], tile))
        return mapping

    def apply_terrain(self, name):
        info = TerrainInfo(os.path.join(self.basedir, '{}.dat'.format(name)))
        return info.get_terrain()
